/*
 * NetClient.h
 *      Author: DePhille
 */

#ifndef NETCLIENT_H_
#define NETCLIENT_H_


// Include:
#include "Log.h"
#include "HTTPResponse.h"

// Forward declarations:
typedef struct PC_HTTPRequest PC_HTTPRequest;

// Default values and settings:
#define PC_NET_USERAGENT		"PidginCote 1.0"
#define PC_NET_SERVER			"pidgincote.artillery.be"
#define	PC_NET_MAXSIMREQUESTS	3

// Operation names (serverside):
#define PC_NET_SERVER_REQUESTTOKEN	"/server/RequestToken"
#define PC_NET_SERVER_UPLOAD		"/server/UploadFile"
#define PC_NET_SERVER_DOWNLOAD		"/server/DownloadFile"
#define PC_NET_SERVER_LOGLIST		"/server/LogList"

// Queues:
#define	PC_NET_QUEUE_FAIL		0
#define	PC_NET_QUEUE_PENDING	1
#define	PC_NET_QUEUE_WAITING	2

// Data members:
gchar*	pc_net_token;		// TODO: Check if necessary.
long	pc_net_timeDiff;	// TODO: Check if necessary.

// Queues:
static GList*	pc_net_queue_pending;
static GList*	pc_net_queue_waiting;

// HTTP request callback:
typedef void(*PC_Net_HTTP_CallBack)(const PC_HTTPResponse*, gpointer);



// Operations:
static void 			PC_Net_Upload(const PC_Log* log);
static void 			PC_Net_UploadCB(const PC_HTTPResponse* response, gpointer userData);
static void 			PC_Net_Download(PC_Log* log);
static void 			PC_Net_DownloadCB(const PC_HTTPResponse* response, gpointer userData);
static void 			PC_Net_Login(const gchar* username, const gchar* password);
static void 			PC_Net_LoginCB(const PC_HTTPResponse* response, gpointer userData);
static void 			PC_Net_LogList(const gchar* protocol, const gchar* account);
static void 			PC_Net_LogListCB(const PC_HTTPResponse* response, gpointer userData);
static gboolean			PC_Net_LoggedIn(void);

// Methods:
static void				PC_Net_SendRequest(PC_HTTPRequest* request);
static void 			PC_Net_SendRequestCB(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message);
static unsigned short	PC_Net_AddToQueue(PC_HTTPRequest* request);
static void				PC_Net_UnqueueRequests(void);
static gboolean			PC_Net_CheckTimeOuts(gpointer data);
static void				PC_Net_RetryRequest(PC_HTTPRequest* request);
static void				PC_Net_Init();

#endif /* NETCLIENT_H_ */
